//
//  TradeOrderUserCenterViewController.swift
//  Exchange
//
//  Created by rui on 2018/5/22.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class TradeOrderUserCenterViewController: BaseViewController {
    
    let kTradeUserStyleOneCell = "TradeOrderUserCenterOneCell"
    let kTradeUserStyleTwoCell = "TradeOrderUserCenterTwoCell"
    
    lazy var viewModel:TradeOrderUserCenterViewModel = {
        let vm = TradeOrderUserCenterViewModel()
        vm.response = {[weak self](state,isSuccess,errorMsg)in
            
            if isSuccess {
                //刷新头部信息
                if state == ResponseState.trade_ad_userInfo  {
                    self?.reloadHeader()
                }else if state == ResponseState.mine_defriend {
                    CYMBShow.showMessage(kText("屏蔽用户成功"))
                    self?.headerView.isShield = true
                }else if state == ResponseState.mine_trust {
                    CYMBShow.showMessage(kText("信任用户成功"))
                    self?.headerView.isTrust = true
                }else if state == ResponseState.mine_cancel_trust {
                    CYMBShow.showMessage(kText("取消信任用户成功"))
                    self?.headerView.isTrust = false
                }else if state == ResponseState.mine_cancel_defriend {
                    CYMBShow.showMessage(kText("取消屏蔽用户成功"))
                    self?.headerView.isShield = false
                }
            }else {
                if let msg = errorMsg {
                    CYMBShow.showMessage(msg)
                }else {
                    CYMBShow.showMessage(kText("当前无网络,请检查网络"))
                }
            }
            
        }
        return vm
    }()
    
    lazy var headerView: TradeOrderUserCenterHeaderView = {
        let headerView = TradeOrderUserCenterHeaderView.loadFromNib() as! TradeOrderUserCenterHeaderView
        headerView.frame = CGRect(x: 0, y: 0, width: kSCREEN_WIDTH, height: 130)
        headerView.delegate = self
        return headerView
    }()
    
    lazy var tabbar: TabbarView = {
        let tabbar = TabbarView(titles: [kText("他发布的广告"),kText("我和他的交易")], pages: ["UserCenterPageController","UserCenterPageController"], pageData: ["userId":viewModel.userId],headerType: .underLine)
        tabbar.frame = CGRect(x: 0, y: headerView.bottom, width: kSCREEN_WIDTH, height: self.view.height - headerView.bottom)
        tabbar.autoresizingMask = .flexibleHeight
        return tabbar
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        viewModel.loadUserInfo()
    }
    func initUI() {
        self.view.addSubview(headerView)
        self.view.addSubview(tabbar)
        self.navigationController?.darkTheme()
    }
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        headerView.height = 130
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        UIApplication.shared.statusBarStyle = .lightContent
        self.tabbar.getCurPageController()?.viewWillAppear()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.tabbar.getCurPageController()?.viewWillDisappear()
    }
    
    func reloadHeader() {
        if let model = viewModel.userInfoModel {
            self.headerView.configerModel(model: model)
        }
    }
}

extension TradeOrderUserCenterViewController: OrderUserCenterHeaderDelegate {
    ///屏蔽点击
    func shieldClick(header: TradeOrderUserCenterHeaderView) {
        if header.isShield {
            self.viewModel.cancleDefriend()
        }else {
            self.viewModel.defriend()
        }
    }
    ///信任按钮点击
    func trustClick(header: TradeOrderUserCenterHeaderView) {

        if header.isTrust {
            self.viewModel.cancelTrust()
        }else {
            self.viewModel.trust()
        }
    }
}
    



